package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.Activity.Main2Activity;
import com.example.myapplication.Activity.Main5Activity;
import com.example.myapplication.fragment.f2;
import com.example.myapplication.fragment.f3;
import com.example.myapplication.fragment.f4;
import com.example.myapplication.fragment.f4second;
import com.example.myapplication.fragment.f4third;
import com.example.myapplication.fragment.f6;
import com.example.myapplication.fragment.f7;
import com.example.myapplication.fragment.f8;
import com.example.myapplication.fragment.fragment2;

public class MainActivity extends AppCompatActivity {

    private EditText editHeight;
    private EditText editWeight;
    private RadioButton rbBoy;
    private RadioButton rbGirl;
    private Button buttonStart;
    private Button buttonReset;
    private ListView zouListview;
    String ee[] = new String[8];
    ArrayAdapter arrayAdapter;
    private DrawerLayout drawlayout;
    private LinearLayout layout;
    FragmentManager fm;
    fragment2 fragment2;
    f2 f2;f3 f3;
    f4 f4;f4third f4third;f4second f4second;
    f6 f6;
    f7 f7;
    f8 f8;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.set);
        }
        initDate();
        loadDefaultFragment();
    }

    /*
    初始化侧滑菜单
     */
    public void initDate() {
        ee[0] = "bmi的逻辑计算";
        ee[1] = "注册页面";
        ee[2]="计算器页面";
        ee[3]="猜拳游戏";
        ee[4]="轮播滚动图";
        ee[5]="书籍列表二级联动";
        ee[6]="信息列表展示";
        ee[7]="照片墙效果";

        arrayAdapter = new ArrayAdapter(this, android.R.layout.activity_list_item, android.R.id.text1, ee);
        zouListview.setAdapter(arrayAdapter);
        zouListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                drawlayout.closeDrawer(Gravity.LEFT);
                switch (position) {
                    case 0:
               if (fragment2==null){
                   fragment2=new fragment2();
               }
                    replaceFragment(fragment2);
                        break;
                    case 1:
                        if (f2==null){
                            f2=new f2();
                        }
                        replaceFragment(f2);
                        break;
                    case 2:
                        if (f3==null){
                            f3=new f3();
                        }
                        replaceFragment(f3);
                        break;
                    case 3:
//                        if (f4==null){
//                            f4=new f4();
//                        }
//                        replaceFragment(f4);
                        startActivity(new Intent(MainActivity.this, Main2Activity.class));
                        break;
                    case 4:
                        startActivity(new Intent(MainActivity.this, Main5Activity.class));
                        break;
                    case 5:
                        if (f6==null){
                            f6=new f6();
                        }
                        replaceFragment(f6);
                        break;
                    case 6:
                        if (f7==null){
                            f7=new f7();
                        }
                        replaceFragment(f7);
                        break;
                    case 7:
                        if (f8==null){
                            f8=new f8();
                        }
                        replaceFragment(f8);
                        break;
                }

            }
        });
    }

    /*
    点击打开侧滑菜单
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if(drawlayout.isDrawerOpen(Gravity.LEFT)) {
                    drawlayout.closeDrawer(Gravity.LEFT);
                }else {
                    drawlayout.openDrawer(Gravity.LEFT);
                }
        }
        return true;
    }


    /*
    加载第一个页面
     */
    void loadDefaultFragment() {
//定义管理器
        fm = getSupportFragmentManager();
//定义事务对象
        FragmentTransaction ft = fm.beginTransaction();
//添加Fragment
        fragment2 = new fragment2();
        ft.add(R.id.layout, fragment2);
        nowfragment=fragment2;
        ft.commit();
    }
/*
切换界面
 */
Fragment nowfragment;
void replaceFragment(Fragment fragment){
    fm=getSupportFragmentManager();
    FragmentTransaction ft=fm.beginTransaction();
    if (fragment.isAdded()){
        ft.hide(nowfragment).show(fragment);
    }else {
        ft.add(R.id.layout,fragment).hide(nowfragment);
        ft.addToBackStack(null);
    }
    nowfragment=fragment;
    ft.commit();
}
    /*
    初始化控件
     */
    private void initView() {
        zouListview = (ListView) findViewById(R.id.zouListview);
        drawlayout = (DrawerLayout) findViewById(R.id.drawlayout);//要有固定的宽度
        layout = (LinearLayout) findViewById(R.id.layout);
    }

//    @Override
//    public void change(String game) {
//        if (f4second==null){
//            f4second= f4second.getInstance(game);
//        }
//        Log.d("cccccc", "change1: game2"+game);
//        fm=getSupportFragmentManager();
//        FragmentTransaction ft=fm.beginTransaction();
//        ft.remove(f4);
//
//        replaceFragment(f4second);
//
//    }
//
//    @Override
//    public void change2(String game) {
//        if (f4third==null){
//            f4third= f4third.getInstance(game);
//        }
//        Log.d("cccccc", "change2: game2"+game);
//        fm=getSupportFragmentManager();
//        FragmentTransaction ft=fm.beginTransaction();
//        ft.remove(f4second);
//        replaceFragment(f4third);
//    }
//
//    @Override
//    public void change3() {
//        if (f4==null){
//            f4=new f4();
//        }
//        fm=getSupportFragmentManager();
//        FragmentTransaction ft=fm.beginTransaction();
//        ft.remove(f4third);
//        replaceFragment(f4);
//    }
}
